/*
 * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package com.dc.gameserver.serverCore.service.battle.coreBattleLogic.sameCardType;

import com.dc.gameserver.baseConfig.Config;
import com.dc.gameserver.exceptions.logicException;
import com.dc.gameserver.hardCode.CardType;
import com.dc.gameserver.serverCore.service.battle.GameFormula;
import com.dc.gameserver.serverCore.service.battle.coreBattleLogic.AbstractSameLogic;
import com.dc.gameserver.serverCore.service.cardModule.Card;
import com.dc.gameserver.serverCore.service.cardModule.CardBox;
import com.dc.gameserver.serverCore.service.cardModule.CardBoxManager;
import com.dc.gameserver.serverCore.service.character.GameCharacter;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 石头哥哥
 *         Date: 13-11-1</br>
 *         Time: 上午9:58</br>
 *         Package: Server.ServerCore.Service.fightProcess.sameCardType</br>
 *         都出攻击牌逻辑
 *         注解：玩家都出 攻击 3  逻辑处理
 */
@Service
public class attackLogic extends AbstractSameLogic {

    /**
     *
     * 通过spring注解方式初始化处理logic的实例化引用
     */
    @Override
    public void PostConstruct() {
        LOGICS[CardType.ATTACK_TYPE.getType()]=this;
    }


    /**
     * logic 处理
     * 双方同出攻击牌时，通过点数判定成功(rule);
     * @param p1 玩家1 出攻击牌
     * @param p2 玩家2 出攻击牌
     * 都出攻击牌，通过点数大小来判定
     */
    @Override
    public void logicHandle(GameCharacter p1,GameCharacter p2) {
        CardBox cardBox= CardBoxManager.getCardBoxByID(p1.getCardBoxID());
        List<Card> out1=p1.getOutCardsList();
        List<Card> out2=p2.getOutCardsList();
        if (!out1.isEmpty()&&!out2.isEmpty()){
            int point1=0,point2=0;
            for (com.dc.gameserver.serverCore.service.cardModule.Card card:out1){
                point1+=card.getCount();
            }
            for (com.dc.gameserver.serverCore.service.cardModule.Card card:out2){
                point2+=card.getCount();
            }
            //相克属性判定
            if (point1>point2){
                //p1--attack-->p2
                int garms= GameFormula.grams(p1.getSelfFiveType(), p2.getSelfFiveType());
                int hurt=0;
//                int p2Blood=p2.getCurrentBlood();
                int p2Shield=p2.getCurrentShield();
                int p2Defence=p2.getDefence();
                int p1Attack=p1.getAttack();
                if (garms== 0x1){
                    hurt=GameFormula.attackHurtWithGrams(p2Defence, p1Attack);
                }else if (garms== 0x2){
                    hurt=GameFormula.attackHurtWithUnGrams(p2Defence, p1Attack);
                } else {
                    hurt=GameFormula.attackHurtWithGramsed(p2Defence, p1Attack);
                }
                if (hurt<=0){
                    hurt=0;
                    hurt= Config.DEFAULT_VALUE.GAME_VALUE.hurt;
                }
                //p1对p2进行物理攻击
//                if (p2Shield>hurt){
//                    p2.setCurrentShield(p2Shield-hurt);
//                } else {
//                    if (p2Shield==0){
//                        p2.setCurrentBlood(p2Blood-hurt);
//                    }else {
//                        p2.setCurrentBlood(p2Blood+p2Shield-hurt);
//                        p2.setCurrentShield(0);  //next time will be 0
//                    }
//                }
                p2.setCurrentShield(p2Shield-hurt);
                cardBox.sendHurtRule(p2.getID(),hurt,p1.getBattleSkillID());
                if (p2.getCurrentShield()<=0){
                    p2.setCurrentShield(0);
//                    p2.setCurrentBlood(0);
                    //被攻击掉血
                    cardBox.flushBloodAndMagic();
                    cardBox.notifyPkResult(p1);
                    CardBoxManager.clearPkResourceWithRoundOver(cardBox.getCardBoxID());
                } else {
                    cardBox.flushBloodAndMagic();
                    cardBox.resetRoundResource();
                }
            } else if(point1<point2){
                //p2--attack-->p1
                int garms= GameFormula.grams(p2.getSelfFiveType(), p1.getSelfFiveType());
                int hurt=0;
//                int p1Blood=p1.getCurrentBlood();
                int p1Shield=p1.getCurrentShield();
                int p1Defence=p1.getDefence();
                int p2Attack=p2.getAttack();
                if (garms== 0x1){
                    hurt=GameFormula.attackHurtWithGrams(p1Defence, p2Attack);
                }else if (garms== 0x2){
                    hurt=GameFormula.attackHurtWithUnGrams(p1Defence, p2Attack);
                } else {
                    hurt=GameFormula.attackHurtWithGramsed(p1Defence, p2Attack);
                }
                if (hurt<=0){
                    hurt=0;
                    hurt= Config.DEFAULT_VALUE.GAME_VALUE.hurt;
                }
                //p2对p1进行物理攻击
//                if (p1Shield>hurt){
//                    p1.setCurrentShield(p1Shield-hurt);
//                } else {
//                    if (p1Shield==0){
//                        p1.setCurrentBlood(p1Blood-hurt);
//                    }else {
//                        p1.setCurrentBlood(p1Blood+p1Shield-hurt);
//                        p1.setCurrentShield(0);  //next time will be 0
//                    }
//                }
                //同步伤害
                p1.setCurrentShield(p1Shield-hurt);
                cardBox.sendHurtRule(p1.getID(),hurt,p2.getBattleSkillID());
                if (p1.getCurrentShield()<=0){
                    p1.setCurrentShield(0);
//                    p1.setCurrentBlood(0);
                    //被攻击掉血
                    cardBox.flushBloodAndMagic();
                    cardBox.notifyPkResult(p2);
                    CardBoxManager.clearPkResourceWithRoundOver(cardBox.getCardBoxID());
                } else {
                    cardBox.flushBloodAndMagic();
                    cardBox.resetRoundResource();
                }
            } else if (point1==point2){
                cardBox.flushBloodAndMagic();
                cardBox.resetRoundResource();
            }
        }else {
            throw new logicException("out1 ,out2 size not 0, please check logic");
        }
    }

}
